package com.sunday.pinxinghui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunday.common.utils.DeviceUtils;
import com.sunday.common.utils.PixUtils;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.entity.IndexProduct;
import com.sunday.pinxinghui.entity.Product;
import com.sunday.pinxinghui.model.products.ProductDetailActivity;

import java.math.RoundingMode;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/8/17.
 */

public class IndexTodaysAdapter extends RecyclerView.Adapter {


    private Context mContext;
    private List<IndexProduct> productList;
    private int type = 2;//1已结束 2进行中 3预热中
    private long timeLimitId;
    private int appWidth;

    public IndexTodaysAdapter(Context context, List<IndexProduct> products) {
        mContext = context;
        productList = products;
        appWidth = DeviceUtils.getDisplay(mContext).widthPixels;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_index_product_item, null);
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        ListHolder productHolder = (ListHolder) holder;
        IndexProduct item1 = productList.get(position);
        if (!TextUtils.isEmpty(item1.getImage())) {
            Glide.with(mContext)
                    .load(item1.getImage())
                    .error(R.mipmap.ic_default)
                    .into(productHolder.mainImg);
        }
        productHolder.productTitle.setText(item1.getProductName());
        productHolder.sellOutImg.setVisibility(item1.getStore() == 0 ? View.VISIBLE : View.GONE);
        switch (type) {
            case 1:
                productHolder.btnBuy.setText("已结束");
                break;
            case 2:
                productHolder.btnBuy.setText("立即抢购");
                break;
            case 3:
                productHolder.btnBuy.setText("预热中");
                break;
        }
        productHolder.priceNow.setText(String.format("¥%s", item1.getPrice().setScale(2, RoundingMode.HALF_UP)));
        productHolder.priceOld.setText(String.format("¥%s", item1.getCostPrice().setScale(2, RoundingMode.HALF_UP)));
        productHolder.priceOld.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        productHolder.totalLayout.setTag(item1);
    }


    @Override
    public int getItemCount() {
        return productList.size();
    }

    public void setTimeLimitId(long timeLimitId) {
        this.timeLimitId = timeLimitId;
    }

    public void setType(int type) {
        this.type = type;
    }


    class ListHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @Bind(R.id.main_img)
        ImageView mainImg;
        @Bind(R.id.product_title)
        TextView productTitle;
        @Bind(R.id.price_now)
        TextView priceNow;
        @Bind(R.id.price_old)
        TextView priceOld;
        @Bind(R.id.btn_buy)
        TextView btnBuy;
        @Bind(R.id.total_layout)
        RelativeLayout totalLayout;
        @Bind(R.id.sell_out_img)
        ImageView sellOutImg;

        public ListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            totalLayout.setOnClickListener(this);
            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mainImg.getLayoutParams();
            layoutParams.width = DeviceUtils.getDisplay(mContext).widthPixels - PixUtils.dip2px(mContext, 20);
            layoutParams.height = (int) (layoutParams.width / 2.7);
            mainImg.setLayoutParams(layoutParams);

        }

        @Override
        public void onClick(View v) {
            IndexProduct item = (IndexProduct) v.getTag();
            Intent intent = new Intent(mContext, ProductDetailActivity.class);
            //intent.putExtra("type", );
            intent.putExtra("productId", item.getProductId());
            //intent.putExtra("limitId", timeLimitId);
            mContext.startActivity(intent);
        }
    }


}
